#include "lib/Config.h"
#include "lib/STC32G_GPIO.h"
#include "lib/STC32G_UART.h"
#include "lib/STC32G_NVIC.h"
#include "ASR.h"

uint32 *pASRcmd;
uint8 ASRDebugBuff[ASR_DEBUG_LENTH];

void ASRInitialize(void){
	GPIO_InitTypeDef gpio;
	COMx_InitDefine com1;
	
	gpio.Pin = GPIO_Pin_6|GPIO_Pin_7;
	gpio.Mode = GPIO_PullUp;
	
	GPIO_Inilize(GPIO_P3,&gpio);
	
	com1.UART_Mode = UART_8bit_BRTx;
	com1.UART_BRT_Use = BRT_Timer1;
	com1.UART_BaudRate = 9600ul;
	com1.UART_RxEnable = ENABLE;
	com1.BaudRateDouble = DISABLE;
	UART_Configuration(UART1,&com1);
	
	NVIC_UART1_Init(ENABLE,Priority_1);
	
	UART1_SW(UART1_SW_P36_P37);
	
	pASRcmd = (uint32 *)RX1_Buffer;
}

//UART1 systick callback(1ms)
//UART1 timeout reset RX_Cnt
void SystickCallbackUART1(void){
	if(--COM1.RX_TimeOut <= 0)
		COM1.RX_Cnt = 0;
}

//ASR systick callback(100ms)
//Analysis ASR command and run
//update ASR debug buffer
void SystickCallbackASR(void){
	switch(*pASRcmd)
	{
		case(ASR_NULL):
			break;
		case(ASR_WAKEUP):
			sprintf(ASRDebugBuff,"ASR Wakeup!");
			break;
		case(ASR_LED_TURNON):
			sprintf(ASRDebugBuff,"Turn on led!");
			LEDTurnOn(LED_MODE_ON);
			break;
		case(ASR_LED_TURNOFF):
			sprintf(ASRDebugBuff,"Turn off led!");
			LEDTurnOn(LED_MODE_OFF);
			break;
		case(ASR_LED_BRUP):
			sprintf(ASRDebugBuff," ");
			break;
		case(ASR_LED_BRDOWN):
			sprintf(ASRDebugBuff," ");
			break;
		case(ASR_BLED_TURNON):
			BLEDTurnOn(BLED_MODE_COLOR);
			sprintf(ASRDebugBuff,"Turn on Bled!");
			break;
		case(ASR_BLED_TURNOFF):
			BLEDTurnOn(BLED_MODE_OFF);
			sprintf(ASRDebugBuff,"Turn off Bled!");
			break;
		case(ASR_BLED_RED):
			BLEDTurnOn(BLED_MODE_RED);
			sprintf(ASRDebugBuff,"Bled switch red!");
			break;
		case(ASR_BLED_GREEN):
			BLEDTurnOn(BLED_MODE_GREEN);
			sprintf(ASRDebugBuff,"Bled switch green!");
			break;
		case(ASR_BLED_BULE):
			BLEDTurnOn(BLED_MODE_BLUE);
			sprintf(ASRDebugBuff,"Bled switch blue!");
			break;
		case(ASR_BLED_COLOR):
			BLEDTurnOn(BLED_MODE_COLOR);
			sprintf(ASRDebugBuff,"Bled switch color!");
			break;
		case(ASR_MOTOR_START):
			MotorStart();
			sprintf(ASRDebugBuff,"Start motor!");
			break;
		case(ASR_MOTOR_STOP):
			MotorStop();
			sprintf(ASRDebugBuff,"Stop motor");
			break;
		case(ASR_MOTOR_ACC):
			MotorAccelerate();
			sprintf(ASRDebugBuff,"Motor Accelerate!");
			break;
		case(ASR_MOTOR_DEC):
			MotorDecelerate();
			sprintf(ASRDebugBuff,"Motor Decelerate!");
			break;
		case(ASR_MCU_RESET):
			UserReset();
			sprintf(ASRDebugBuff,"Reset MCU!");
			break;
		case(ASR_MCU_ISP):
			UserISP();
			sprintf(ASRDebugBuff,"Program MCU!");
			break;
		case(ASR_DISPLAY_HOME):
			pCurrentMenu = MenuPrcHome;
			sprintf(ASRDebugBuff,"Display home!");
			break;
		case(ASR_DISPLAY_ASR):
			pCurrentMenu = MenuPrcASR;
			sprintf(ASRDebugBuff,"Display ASR!");
			break;
		case(ASR_DISPLAY_JOYS):
			pCurrentMenu = MenuPrcJoys;
			sprintf(ASRDebugBuff,"Display joystick!");
			break;
		case(ASR_DISPLAY_BLED):
			pCurrentMenu = MenuPrcBLED;
			sprintf(ASRDebugBuff,"Display Breath led!");
			break;
		case(ASR_DISPLAY_MORTOR):
			pCurrentMenu = MenuPrcMotor;
			sprintf(ASRDebugBuff,"Display motor!");
			break;
		case(ASR_DISPLAY_IMU):
			pCurrentMenu = MenuPrcIMU;
			sprintf(ASRDebugBuff,"Display IMU!");
			break;
		case(ASR_DISPLAY_DEBUG):
			pCurrentMenu = MenuPrcDebug;
			sprintf(ASRDebugBuff,"Display debug!");
			break;
		case(ASR_JOYS_CALI_LEFT):
			JoystickCalibrate(JOYS_CALI_XMIN);
			sprintf(ASRDebugBuff,"Joystick left calibrated!");
			break;
		case(ASR_JOYS_CALI_RIGHT):
			JoystickCalibrate(JOYS_CALI_XMAX);
			sprintf(ASRDebugBuff,"Joystick right calibrated!");
			break;
		case(ASR_JOYS_CALI_UP):
			JoystickCalibrate(JOYS_CALI_YMIN);
			sprintf(ASRDebugBuff,"Joystick up calibrated!");
			break;
		case(ASR_JOYS_CALI_DOWN):
			JoystickCalibrate(JOYS_CALI_YMAX);
			sprintf(ASRDebugBuff,"Joystick down calibrated!");
			break;
		case(ASR_JOYS_CALI_MID):
			JoystickCalibrate(JOYS_CALI_MID);
			sprintf(ASRDebugBuff,"Joystick middle calibrated!");
			break;
		
		default:
			sprintf(ASRDebugBuff,"ASR:Wrong command!");
			break;
	}
}